package com.banshuibao.bsb_android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.util.PriceUtil;

/**
 * Created by zyh on 2018/5/23.
 */

public class CounterActivity extends BaseActivity {

    private static final int REQUEST_TAX_TYPE = 1000;
    private static final int REQUEST_FIRST_MONEY = 1001;
    private static final int REQUEST_TIME_TYPE = 1002;

    private static final int TYPE_WAGES = 0;//工资、薪金所得
    private static final int TYPE_YEAR_AWARD = 1;//年终奖
    private static final int TYPE_LABOUR = 2;//劳务所得
    private static final int TYPE_COST = 3;//个体公商务生产、经营所得

    private TextView tax_income, tax_pay, tax_pay_money, tax_rate, tax_take_off;

    private TextView income_type, first_type, rest, compute, time_type;

    private EditText safe_money, income_money, cost_money;

    private View safe_layout, safe_line, first_layout, first_line, cost_layout, cost_line, time_layout, time_line;

    private int type = TYPE_WAGES;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_counter);

        initView();
    }

    private void initView() {
        setTitleText("个税计算器");
        findViewById(R.id.relative).setBackgroundResource(R.drawable.icon_counter_tite_bg);

        tax_income = findViewById(R.id.tax_income);
        tax_income.getPaint().setFakeBoldText(true);
        tax_pay = findViewById(R.id.tax_pay);
        tax_pay.getPaint().setFakeBoldText(true);
        tax_pay_money = findViewById(R.id.tax_pay_money);
        tax_pay_money.getPaint().setFakeBoldText(true);
        tax_rate = findViewById(R.id.tax_rate);
        tax_rate.getPaint().setFakeBoldText(true);
        tax_take_off = findViewById(R.id.tax_take_off);
        tax_take_off.getPaint().setFakeBoldText(true);

        income_type = findViewById(R.id.income_type);
        income_type.setOnClickListener(l);
        first_type = findViewById(R.id.first_type);
        first_type.setOnClickListener(l);
        time_type = findViewById(R.id.time_type);
        time_type.setOnClickListener(l);
        rest = findViewById(R.id.rest);
        rest.setOnClickListener(l);
        compute = findViewById(R.id.compute);
        compute.setOnClickListener(l);
        safe_money = findViewById(R.id.safe_money);
        income_money = findViewById(R.id.income_money);
        income_money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                compute.setEnabled(!TextUtils.isEmpty(s));
            }
        });

        safe_layout = findViewById(R.id.safe_layout);
        safe_line = findViewById(R.id.safe_line);
        first_layout = findViewById(R.id.first_layout);
        first_line = findViewById(R.id.first_line);
        cost_money = findViewById(R.id.cost_money);
        cost_layout = findViewById(R.id.cost_layout);
        cost_line = findViewById(R.id.cost_line);
        time_layout = findViewById(R.id.time_layout);
        time_line = findViewById(R.id.time_line);
    }

    private View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.income_type:
                    Intent it = new Intent(context, TaxTypeActivity.class);
                    it.putExtra("type", type);
                    startActivityForResult(it, REQUEST_TAX_TYPE);
                    break;
                case R.id.first_type:
                    Intent it1 = new Intent(context, TaxFirstActivity.class);
                    it1.putExtra("money", first_type.getText().toString());
                    startActivityForResult(it1, REQUEST_FIRST_MONEY);
                    break;
                case R.id.time_type:
                    Intent it2 = new Intent(context, TaxTimeTypeActivity.class);
                    it2.putExtra("time", time_type.getText().toString().replace("个月", ""));
                    startActivityForResult(it2, REQUEST_TIME_TYPE);
                    break;
                case R.id.rest:
                    setDefault();
                    break;
                case R.id.compute:
                    compute();
                    break;
            }
        }
    };

    private void showWagesView(int visible) {
        safe_layout.setVisibility(visible);
        safe_line.setVisibility(visible);
        first_layout.setVisibility(visible);
        first_line.setVisibility(visible);
    }

    private void showCostView(int visible) {
        cost_layout.setVisibility(visible);
        cost_line.setVisibility(visible);
        time_layout.setVisibility(visible);
        time_line.setVisibility(visible);
    }

    private void chooseType(int type) {
        if (type == TYPE_WAGES) {
            showWagesView(View.VISIBLE);
            showCostView(View.GONE);
        } else if (type == TYPE_COST) {
            showWagesView(View.GONE);
            showCostView(View.VISIBLE);
        } else {
            showWagesView(View.GONE);
            showCostView(View.GONE);
        }
        this.type = type;
        setDefault();
    }

    private void setDefault() {
        tax_income.setText("0.00");
        tax_pay.setText("0.00");
        tax_pay_money.setText("0.00");
        tax_rate.setText("0%");
        tax_take_off.setText("0.00");

        income_money.setText("");
        safe_money.setText("");
        first_type.setText("3500");
        cost_money.setText("");
        time_type.setText("1个月");
    }

    private void setCompute(double taxIncome, double taxPay, double taxPayMoney, double taxRate, double taxTakeOff) {
        tax_income.setText(PriceUtil.FormatDecimal(taxIncome));
        tax_pay.setText(PriceUtil.FormatDecimal(taxPay));
        tax_pay_money.setText(PriceUtil.FormatDecimal(taxPayMoney));
        tax_rate.setText(String.valueOf(taxRate * 100 + "%"));
        tax_take_off.setText(PriceUtil.FormatDecimal(taxTakeOff));
    }

    private Double getIncomeMoney() {
        return Double.valueOf(income_money.getText().toString());
    }

    private void compute() {
        switch (type) {
            case TYPE_WAGES:
                computeWages();
                break;
            case TYPE_YEAR_AWARD:
                computeYearAward();
                break;
            case TYPE_LABOUR:
                computeLabour();
                break;
            case TYPE_COST:
                computeCost();
                break;
        }
    }

    /*
    * 计算工资薪金
    * **/
    private void computeWages() {
        double incomeMoney = getIncomeMoney();
        double safeMoney = Double.valueOf(TextUtils.isEmpty(safe_money.getText().toString()) ? "0" : safe_money.getText().toString());
        double firstMoney = Double.valueOf(first_type.getText().toString());
        //应缴税额
        double taxPayMoney = incomeMoney - safeMoney - firstMoney;
        if (taxPayMoney < 0) {
            taxPayMoney = 0;
        }
        //税率
        double taxRate;
        //速算扣除
        double taxTakeOff;
        if (taxPayMoney <= 1500) {
            taxRate = 0.03;
            taxTakeOff = 0;
        } else if (taxPayMoney <= 4500) {
            taxRate = 0.1;
            taxTakeOff = 105;
        } else if (taxPayMoney <= 9000) {
            taxRate = 0.2;
            taxTakeOff = 555;
        } else if (taxPayMoney <= 35000) {
            taxRate = 0.25;
            taxTakeOff = 1005;
        } else if (taxPayMoney <= 55000) {
            taxRate = 0.3;
            taxTakeOff = 2755;
        } else if (taxPayMoney <= 80000) {
            taxRate = 0.35;
            taxTakeOff = 5505;
        } else {
            taxRate = 0.45;
            taxTakeOff = 13505;
        }
        //应缴个税
        double taxPay = taxPayMoney * taxRate - taxTakeOff;
        if (taxPay <= 0) {
            taxPay = 0;
            taxRate = 0;
            taxTakeOff = 0;
        }
        //税后收入
        double taxIncome = incomeMoney - taxPay - safeMoney;
        if (taxIncome < 0)
            taxIncome = 0;
        setCompute(taxIncome, taxPay, taxPayMoney, taxRate, taxTakeOff);
    }

    /*
   * 计算年终奖
   * **/
    private void computeYearAward() {
        double incomeMoney = getIncomeMoney();
        double incomeMoneyMonth = incomeMoney / 12;
        //税率
        double taxRate;
        //速算扣除
        double taxTakeOff;
        if (incomeMoneyMonth <= 1500) {
            taxRate = 0.03;
            taxTakeOff = 0;
        } else if (incomeMoneyMonth <= 4500) {
            taxRate = 0.1;
            taxTakeOff = 105;
        } else if (incomeMoneyMonth <= 9000) {
            taxRate = 0.2;
            taxTakeOff = 555;
        } else if (incomeMoneyMonth <= 35000) {
            taxRate = 0.25;
            taxTakeOff = 1005;
        } else if (incomeMoneyMonth <= 55000) {
            taxRate = 0.3;
            taxTakeOff = 2755;
        } else if (incomeMoneyMonth <= 80000) {
            taxRate = 0.35;
            taxTakeOff = 5505;
        } else {
            taxRate = 0.45;
            taxTakeOff = 13505;
        }
        double taxPay = incomeMoney * taxRate - taxTakeOff;
        double taxIncome = incomeMoney - taxPay;
        setCompute(taxIncome, taxPay, incomeMoney, taxRate, taxTakeOff);
    }

    /**
     * 计算劳务报酬
     */
    private void computeLabour() {
        double incomeMoney = getIncomeMoney();

        double taxPlayMoney;
        if (incomeMoney <= 800) {
            taxPlayMoney = 0;
        } else if (incomeMoney <= 4000) {
            taxPlayMoney = incomeMoney - 800;
        } else {
            taxPlayMoney = incomeMoney * 0.8;
        }

        double taxRate;
        double taxTakeOff;
        if (incomeMoney <= 20000) {
            taxRate = 0.2;
            taxTakeOff = 0;
        } else if (incomeMoney <= 50000) {
            taxRate = 0.3;
            taxTakeOff = 2000;
        } else {
            taxRate = 0.4;
            taxTakeOff = 7000;
        }

        if (taxPlayMoney <= 0) {
            taxRate = 0;
            taxTakeOff = 0;
        }
        double taxPay = taxPlayMoney * taxRate - taxTakeOff;
        double taxIncome = incomeMoney - taxPay;
        setCompute(taxIncome, taxPay, taxPlayMoney, taxRate, taxTakeOff);
    }

    /**
     * 计算成本
     */
    private void computeCost() {
        double incomeMoney = getIncomeMoney();
        double costMoney = Double.valueOf(TextUtils.isEmpty(cost_money.getText().toString()) ? "0" : cost_money.getText().toString());
        double timeMoney = 3500 * Integer.valueOf(time_type.getText().toString().replace("个月", ""));
        double taxPayMoney = incomeMoney - costMoney - timeMoney;
        if (taxPayMoney < 0) {
            taxPayMoney = 0;
        }

        double taxRate;
        double taxTakeOff;
        if (taxPayMoney <= 15000) {
            taxRate = 0.05;
            taxTakeOff = 0;
        } else if (taxPayMoney <= 30000) {
            taxRate = 0.1;
            taxTakeOff = 750;
        } else if (taxPayMoney <= 60000) {
            taxRate = 0.2;
            taxTakeOff = 3750;
        } else if (taxPayMoney <= 100000) {
            taxRate = 0.3;
            taxTakeOff = 9750;
        } else {
            taxRate = 0.35;
            taxTakeOff = 14750;
        }

        double taxPay = taxPayMoney * taxRate - taxTakeOff;
        if (taxPay <= 0) {
            taxPay = 0;
            taxRate = 0;
            taxTakeOff = 0;
        }
        double taxIncome = incomeMoney - taxPay - costMoney;
        if (taxIncome < 0)
            taxIncome = 0;
        setCompute(taxIncome, taxPay, taxPayMoney, taxRate, taxTakeOff);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_TAX_TYPE:
                    int type = data.getIntExtra("type", 0);
                    if (type != this.type) {
                        chooseType(type);
                        String text = data.getStringExtra("text");
                        income_type.setText(text);
                    }
                    break;
                case REQUEST_FIRST_MONEY:
                    String money = data.getStringExtra("money");
                    first_type.setText(money);
                    break;
                case REQUEST_TIME_TYPE:
                    String time = data.getStringExtra("time");
                    time_type.setText(String.valueOf(time + "个月"));
                    break;
            }
        }
    }
}
